package cn.source.web.controller.zk;

import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.core.page.TableDataInfo;
import cn.source.zk.domain.entity.Channel;
import cn.source.zk.domain.entity.Cxdz;
import cn.source.zk.domain.entity.Device;
import cn.source.zk.domain.entity.Sort;
import cn.source.zk.domain.vo.DeviceAndChannelByTdGuidVO;
import cn.source.zk.service.IZkChannelService;
import cn.source.zk.service.IZkCxdzService;
import cn.source.zk.service.IZkDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/zk/channel")
@Slf4j
public class ZkChannelController extends BaseController {

    @Autowired
    private IZkChannelService channelService;

    @Autowired
    private IZkCxdzService cxdzService;

    @Autowired
    private IZkDeviceService deviceService;


    /**
     * 查询通道分页列表
     * @param channel
     * @return
     */
    @GetMapping("list")
    public TableDataInfo listPage(Channel channel){
        startPage();
        List<Channel> list = channelService.listPage(channel);
        return getDataTable(list);
    }

    /**
     * 新增通道
     * @return
     */
    @Transactional
    @PostMapping
    public AjaxResult add(@RequestBody Channel channel){
        log.info("新增的通道数据:{}",channel);
        //校验通道名称是否存在
        Boolean b = channelService.checkChannelNameExist(channel.getChannelName(), channel.getNum(), channel.getDeviceNum());
        if (b){
            return AjaxResult.error("通道名称"+channel.getChannelName()+"已存在");
        }

        // 校验通道数量是否大于12，因为这是在新增之前检查的等于12的时候也不行
        ArrayList<Long> nums = new ArrayList<>();
        nums.add(channel.getDeviceNum());
        List<Channel> channelList = channelService.getChannelByDeviceNums(nums);
        if (channelList.size()>=12){
            return AjaxResult.error("设备下已有12个通道，不能新增");
        }

        // 更新对应的设备的通道数量
        Device device = new Device();
        device.setNum(channel.getDeviceNum());
        device.setChannelSum(channelList.size()+1);  //是已有的数量，不包含新增的数量
        deviceService.edit(device);

        // 设施创建人和修改人
        channel.setCreateBy(getUsername());
        channel.setUpdateBy(getUsername());
        channel.setTdGuid(UUID.randomUUID().toString());
        // 这里先默认写死，跟客户确认后修改(为db_basedata表中的的一个值)
        //channel.setTdGuid("77E7BCBB-4319-4C65-A835-740325227A0D");

        //同时生成Cxdz
        Cxdz cxdz1 = new Cxdz();
        cxdz1.setTdGuid(channel.getTdGuid());
        cxdz1.setType(0);

        Cxdz cxdz2 = new Cxdz();
        cxdz2.setTdGuid(channel.getTdGuid());
        cxdz2.setType(1);

        cxdzService.add(cxdz1);
        cxdzService.add(cxdz2);

        return toAjax(channelService.add(channel));
    }

    /**
     * 根据num查询通道
     * @param num
     * @return
     */
    @GetMapping("/{num}")
    public AjaxResult getByNum(@PathVariable Long num){
        Channel channel = channelService.getByNum(num);
        return AjaxResult.success(channel);
    }

    /**
     * 修改通道
     * @param channel
     * @return
     */
    @PutMapping()
    public AjaxResult edit(@RequestBody Channel channel){

        //校验通道名称是否存在
        Boolean b = channelService.checkChannelNameExist(channel.getChannelName(), channel.getNum(),channel.getDeviceNum());
        if (b){
            return AjaxResult.error("通道名称"+channel.getChannelName()+"已存在");
        }


        channel.setUpdateBy(getUsername());
        return toAjax(channelService.edit(channel));
    }

    @DeleteMapping("/{nums}")
    public AjaxResult remove(@PathVariable List<Long> nums){
        for (Long num : nums) {
            Channel channel = channelService.getByNum(num);

            ArrayList<Long> devNums = new ArrayList<>();
            devNums.add(channel.getDeviceNum());
            // 查询设备下的通道数量。
            List<Channel> channelList = channelService.getChannelByDeviceNums(devNums);

            // 更新对应的设备的通道数量
            Device device = new Device();
            device.setNum(channel.getDeviceNum());
            device.setChannelSum(channelList.size()-1);
            deviceService.edit(device);

        }

        return toAjax(channelService.remove(nums));
    }

    @GetMapping("/getSortList/{deviceNum}")
    public AjaxResult getSortList(@PathVariable Long deviceNum){

        List<Sort> sortList = channelService.getSortList(deviceNum);

        return AjaxResult.success(sortList);
    }

    @GetMapping("/getDeviceAndChannelByTdGuid/{tdGuid}")
    public AjaxResult getDeviceAndChannelByTdGuid(@PathVariable String tdGuid){
        DeviceAndChannelByTdGuidVO deviceAndChannelByTdGuidVO = channelService.getDeviceAndChannelByTdGuid(tdGuid);
        return AjaxResult.success(deviceAndChannelByTdGuidVO);
    }

}
